<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Embedding Vars in Double Quotes</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="index.html" title="Smarty 3 Manual">
<link rel="up" href="language.basic.syntax.html" title="Chapter 3. Basic Syntax">
<link rel="prev" href="language.syntax.attributes.html" title="Attributes">
<link rel="next" href="language.math.html" title="Math">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Embedding Vars in Double Quotes</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="language.syntax.attributes.html">Prev</a> </td>
<th width="60%" align="center">Chapter 3. Basic Syntax</th>
<td width="20%" align="right"> <a accesskey="n" href="language.math.html">Next</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="sect1" title="Embedding Vars in Double Quotes">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="language.syntax.quotes"></a>Embedding Vars in Double Quotes</h2></div></div></div>
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li class="listitem"><p>
  Smarty will recognize <a class="link" href="api.assign.html" title="assign()">assigned</a>
  <a class="link" href="language.syntax.variables.html" title="Variables">variables</a>
  embedded in "double quotes" so long as the variable name contains only numbers,
  letters and under_scores.
  See <a class="ulink" href="http://php.net/language.variables" target="_top">naming</a>
  for more detail.
  </p></li>
<li class="listitem"><p>
  With any other characters, for example a period(.) or
  <code class="literal">$object-&gt;reference</code>, then the variable must be
  surrounded by  <code class="literal">`backticks`</code>.
  </p></li>
<li class="listitem"><p>
  In addition Smarty3 does allow embedded Smarty tags in double quoted strings.
  This is useful if you want to include variables with modifiers, plugin or PHP function results.
  </p></li>
</ul></div>
<div class="example">
<a name="id344575"></a><p class="title"><b>Example 3.5. Syntax examples</b></p>
<div class="example-contents"><pre class="programlisting">

{func var="test $foo test"}              // sees $foo
{func var="test $foo_bar test"}          // sees $foo_bar
{func var="test `$foo[0]` test"}         // sees $foo[0]
{func var="test `$foo[bar]` test"}       // sees $foo[bar]
{func var="test $foo.bar test"}          // sees $foo (not $foo.bar)
{func var="test `$foo.bar` test"}        // sees $foo.bar
{func var="test `$foo.bar` test"|escape} // modifiers outside quotes!
{func var="test {$foo|escape} test"}     // modifiers inside quotes!
{func var="test {time()} test"}          // PHP function result
{func var="test {counter} test"}         // plugin result
{func var="variable foo is {if !$foo}not {/if} defined"} // Smarty block function

  </pre></div>
</div>
<br class="example-break"><div class="example">
<a name="id344938"></a><p class="title"><b>Example 3.6. Examples</b></p>
<div class="example-contents"><pre class="programlisting">

{* will replace $tpl_name with value *}
{include file="subdir/$tpl_name.tpl"}

{* does NOT replace $tpl_name *}
{include file='subdir/$tpl_name.tpl'} // vars require double quotes!

{* must have backticks as it contains a dot "." *}
{cycle values="one,two,`$smarty.config.myval`"}

{* must have backticks as it contains a dot "." *}
{include file="`$module.contact`.tpl"}

{* can use variable with dot syntax *}
{include file="`$module.$view`.tpl"}

  </pre></div>
</div>
<br class="example-break"><div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>
    Although Smarty can handle some very complex expressions and syntax, it is a good rule of
    thumb to keep the template syntax minimal and focused on presentation. If you find your
    template syntax getting too complex, it may be a good idea to move the bits that do not
    deal explicitly with presentation to PHP by way of plugins or modifiers.
 </p>
</div>
<p>
  See also <a class="link" href="language.modifier.escape.html" title="escape"><code class="varname">escape</code></a>.
 </p>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="language.syntax.attributes.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="language.basic.syntax.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="language.math.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Attributes </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> Math</td>
</tr>
</table>
</div>
</body>
</html>
